package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.Building;
import com.ruoyi.system.mapper.BusinessMapper;
import com.ruoyi.system.domain.Business;
import com.ruoyi.system.service.IBusinessService;

/**
 * 商业区管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-08
 */
@Service
public class BusinessServiceImpl implements IBusinessService 
{
    @Autowired
    private BusinessMapper businessMapper;

    /**
     * 查询商业区管理
     * 
     * @param busId 商业区管理主键
     * @return 商业区管理
     */
    @Override
    public Business selectBusinessByBusId(Long busId)
    {
        return businessMapper.selectBusinessByBusId(busId);
    }

    /**
     * 查询商业区管理列表
     * 
     * @param business 商业区管理
     * @return 商业区管理
     */
    @Override
    public List<Business> selectBusinessList(Business business)
    {
        return businessMapper.selectBusinessList(business);
    }

    /**
     * 新增商业区管理
     * 
     * @param business 商业区管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBusiness(Business business)
    {
        business.setCreateTime(DateUtils.getNowDate());
        int rows = businessMapper.insertBusiness(business);
        insertBuilding(business);
        return rows;
    }

    /**
     * 修改商业区管理
     * 
     * @param business 商业区管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBusiness(Business business)
    {
        business.setUpdateTime(DateUtils.getNowDate());
        businessMapper.deleteBuildingByBusId(business.getBusId());
        insertBuilding(business);
        return businessMapper.updateBusiness(business);
    }

    /**
     * 批量删除商业区管理
     * 
     * @param busIds 需要删除的商业区管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBusinessByBusIds(Long[] busIds)
    {
        businessMapper.deleteBuildingByBusIds(busIds);
        return businessMapper.deleteBusinessByBusIds(busIds);
    }

    /**
     * 删除商业区管理信息
     * 
     * @param busId 商业区管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBusinessByBusId(Long busId)
    {
        businessMapper.deleteBuildingByBusId(busId);
        return businessMapper.deleteBusinessByBusId(busId);
    }

    /**
     * 新增楼栋管理信息
     * 
     * @param business 商业区管理对象
     */
    public void insertBuilding(Business business)
    {
        List<Building> buildingList = business.getBuildingList();
        Long busId = business.getBusId();
        if (StringUtils.isNotNull(buildingList))
        {
            List<Building> list = new ArrayList<Building>();
            for (Building building : buildingList)
            {
                building.setBusId(busId);
                list.add(building);
            }
            if (list.size() > 0)
            {
                businessMapper.batchBuilding(list);
            }
        }
    }
}
